package nju.eai.seller.main;

import java.util.MissingResourceException;

import nju.eai.corba.paymentSystem.SystemC;
import nju.eai.corba.sellerSystem.Date;
import nju.eai.corba.sellerSystem.OrderBill;
import nju.eai.corba.sellerSystem.ProductInfo;
import nju.eai.corba.sellerSystem.PurchaseResponse;
import nju.eai.corba.sellerSystem.SystemB;
import nju.eai.corba.sellerSystem.SystemBHelper;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;

public class SellerClient {

		ORB orb;
		POA rootPOA;
		NamingContextExt ncRef;
		SystemB sb;
		SystemC sc;

		/**
		 * @param name
		 *            the name of CORBA object
		 * @return -return the CORBA Object
		 */
		public SystemB init(String name) {
			String initArgs[] = new String[4];
			try {
				initArgs[0] = "-ORBInitialPort";
				initArgs[1] = "1060";
				initArgs[2] = "-ORBInitialHost";
				initArgs[3] = "localhost";
				// create an ORB instance,telling it the Name Service's location
				this.orb = ORB.init(initArgs, null);

				// get the root naming context
				org.omg.CORBA.Object objRef = orb
						.resolve_initial_references("NameService");
				// Use NamingContextExt instead of NamingContext. This is
				// part of the Interoperable naming Service.
				ncRef = NamingContextExtHelper.narrow(objRef);

				// resolve the Object Reference in Naming
				this.sb = SystemBHelper.narrow(ncRef.resolve_str(name));
				System.out.println("Obtained a handle of CalculateService OBJ.");
				return this.sb;
			} catch (MissingResourceException e) {
				e.printStackTrace();
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}

		public static void main(String[] args) {
			SellerClient slc =  new SellerClient();
			SystemB b =  slc.init("Seller_JAVA");

			if (b != null) {
				System.out.println(b.affirm("200909032553"));
				
							
				
//				//test sentPurchaseExpection
//				ProductInfo pi =  b.sentPurchaseExpection("phone",3000);
//				System.out.println(pi.pid+"   " +pi.pname+"    " + pi.price);
//
//				
//				//test requestPurchase
//				Date adate =  new Date();
//				adate.year = "2009";
//				adate.month = "5";
//				adate.day = "1";
//					PurchaseResponse pr =  b.requestPurchase("200911", 1000, adate);
//					System.out.println(pr.orderno+"   "+pr.isOK);
//
//				
//				
//				
//				//test ackPurchase
//					OrderBill ob =  b.ackPurchase("2009090308");
//					System.out.println(ob.purchaseNo+"   "+ob.totalAmount+"   "+ob.account_of_B);
//
//				
//				
//				//test notifyDealDone
//					b.notifyDealDone("200909050605");
//
//				
				
				//test askToDeliver
			b.askToDeliver("200909050605");

				
				
				
			}
			
			

		}
	


}
